package com.sgcc.ln.pmos.e_ln_mall.presenter;

import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.ln.pmos.common.base.BasePresenter;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvpNetCallback;
import com.sgcc.ln.pmos.e_ln_mall.bean.CurveDataBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.CurveHistoryDataBackBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.CurveHistoryDataBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.QueryHisCurvePriceMonthBean;
import com.sgcc.ln.pmos.e_ln_mall.contract.ElectricityProportionContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class ElectricityProportionPresenter extends BasePresenter<ElectricityProportionContract.View>
        implements ElectricityProportionContract.Presenter {


    /**
     * 电量曲线
     *
     * @param isLoading
     * @param eSource   2 典型曲线  3自定义曲线  4 偏差曲线
     */
    @Override
    public void getCurveData(boolean isLoading, int eSource) {
        getData(eSource, isLoading);
    }


    /**
     * 历史电量曲线
     *
     * @param isLoading
     */
    @Override
    public void getHisCurveData(boolean isLoading, String mktDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("mktDate", mktDate);
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_HIS_CURVE_INFO,
                new MvpNetCallback<CurveDataBean>(getView(), isLoading) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(CurveDataBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        List<String> stringList = new ArrayList<>();
                        if (null != response && null != response.getData() && response.getData().size() > 0) {
                            CurveDataBean.DataDTO data = response.getData().get(0);

                                stringList.add(data.getRatio1());
                                stringList.add(data.getRatio2());
                                stringList.add(data.getRatio3());
                                stringList.add(data.getRatio4());
                                stringList.add(data.getRatio5());
                                stringList.add(data.getRatio6());
                                stringList.add(data.getRatio7());
                                stringList.add(data.getRatio8());
                                stringList.add(data.getRatio9());
                                stringList.add(data.getRatio10());
                                stringList.add(data.getRatio11());
                                stringList.add(data.getRatio12());
                                stringList.add(data.getRatio13());
                                stringList.add(data.getRatio14());
                                stringList.add(data.getRatio15());
                                stringList.add(data.getRatio16());
                                stringList.add(data.getRatio17());
                                stringList.add(data.getRatio18());
                                stringList.add(data.getRatio19());
                                stringList.add(data.getRatio20());
                                stringList.add(data.getRatio21());
                                stringList.add(data.getRatio22());
                                stringList.add(data.getRatio23());
                                stringList.add(data.getRatio24());

                            }



                        getView().queryHisCurveDataSuccess(stringList);

                    }
                });

    }

    /**
     * 选择电量曲线-历史曲线日期
     */
    @Override
    public void getHisCurveDate() {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_HIS_CURVE_MONTH,
                new MvpNetCallback<QueryHisCurvePriceMonthBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(QueryHisCurvePriceMonthBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response)
                            getView().queryHisCurveDateSuccess(response.getData());
                        else {
                            ToastUtils.showShort(response.getMsg());
                        }

                    }
                });


    }

    /**
     * 偏差曲线数据
     *
     * @param isLoading
     * @param eSource   2 典型曲线  3自定义曲线  4 偏差曲线
     */
    @Override
    public void getDeviationCurveData(boolean isLoading, int eSource) {
        getData(eSource, isLoading);
    }


    /**
     * 选择价格-历史价格日期
     */
    @Override
    public void getHisCurvePriceDate() {

        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_HIS_CURVE_PRICE_MONTH,
                new MvpNetCallback<QueryHisCurvePriceMonthBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(QueryHisCurvePriceMonthBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response)
                            getView().queryHisCurveDatePriceSuccess(response.getData());
                        else {
                            ToastUtils.showShort(response.getMsg());
                        }

                    }
                });


    }


    //综合价格日期
    @Override
    public void getSynthesisPriceDate() {

        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_HIS_SYNTHESIS_PRICE_MONTH,
                new MvpNetCallback<QueryHisCurvePriceMonthBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(QueryHisCurvePriceMonthBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response)
                            getView().querySynthesisPriceDateSuccess(response.getData());
                        else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                });

    }

    //查询综合价格
    @Override
    public void getSynthesisPriceData(boolean isLoading, String priceMonth) {

        Map<String, Object> params = new HashMap<>();
        params.put("priceMonth", priceMonth);
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_HIS_SYNTHESIS_PRICE,
                new MvpNetCallback<CurveHistoryDataBean>(getView(), isLoading) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(CurveHistoryDataBean response, int id) {
                        super.onResponse(response, id);
                        List<CurveHistoryDataBackBean> dataList = new ArrayList<>();
                        if (null != response && null != response.getData() && response.getData().size() > 0) {
                            for (CurveHistoryDataBean.DataDTO datum : response.getData()) {
                                List<String> stringList = new ArrayList<>();
                                stringList.add(datum.getPrice1());
                                stringList.add(datum.getPrice2());
                                stringList.add(datum.getPrice3());
                                stringList.add(datum.getPrice4());
                                stringList.add(datum.getPrice5());
                                stringList.add(datum.getPrice6());
                                stringList.add(datum.getPrice7());
                                stringList.add(datum.getPrice8());
                                stringList.add(datum.getPrice9());
                                stringList.add(datum.getPrice10());
                                stringList.add(datum.getPrice11());
                                stringList.add(datum.getPrice12());
                                stringList.add(datum.getPrice13());
                                stringList.add(datum.getPrice14());
                                stringList.add(datum.getPrice15());
                                stringList.add(datum.getPrice16());
                                stringList.add(datum.getPrice17());
                                stringList.add(datum.getPrice18());
                                stringList.add(datum.getPrice19());
                                stringList.add(datum.getPrice20());
                                stringList.add(datum.getPrice21());
                                stringList.add(datum.getPrice22());
                                stringList.add(datum.getPrice23());
                                stringList.add(datum.getPrice24());
                                dataList.add(new CurveHistoryDataBackBean(datum.getDifName(), stringList));
                            }
                        }
                        getView().querySynthesisPriceDataSuccess(dataList);

                    }
                });

    }


    /**
     * 历史价格 数据
     *
     * @param isLoading
     * @param priceMonth 月份
     */
    @Override
    public void getHistoryCurveData(boolean isLoading, String priceMonth) {


        Map<String, Object> params = new HashMap<>();
        params.put("priceMonth", priceMonth);
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_HIS_CURVE_PRICE,
                new MvpNetCallback<CurveHistoryDataBean>(getView(), isLoading) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (isViewAttached()){
                            return;
                        }
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(CurveHistoryDataBean response, int id) {
                        super.onResponse(response, id);
                        List<CurveHistoryDataBackBean> dataList = new ArrayList<>();
                        if (null != response && null != response.getData() && response.getData().size() > 0) {
                            for (CurveHistoryDataBean.DataDTO datum : response.getData()) {
                                List<String> stringList = new ArrayList<>();
                                stringList.add(datum.getPrice1());
                                stringList.add(datum.getPrice2());
                                stringList.add(datum.getPrice3());
                                stringList.add(datum.getPrice4());
                                stringList.add(datum.getPrice5());
                                stringList.add(datum.getPrice6());
                                stringList.add(datum.getPrice7());
                                stringList.add(datum.getPrice8());
                                stringList.add(datum.getPrice9());
                                stringList.add(datum.getPrice10());
                                stringList.add(datum.getPrice11());
                                stringList.add(datum.getPrice12());
                                stringList.add(datum.getPrice13());
                                stringList.add(datum.getPrice14());
                                stringList.add(datum.getPrice15());
                                stringList.add(datum.getPrice16());
                                stringList.add(datum.getPrice17());
                                stringList.add(datum.getPrice18());
                                stringList.add(datum.getPrice19());
                                stringList.add(datum.getPrice20());
                                stringList.add(datum.getPrice21());
                                stringList.add(datum.getPrice22());
                                stringList.add(datum.getPrice23());
                                stringList.add(datum.getPrice24());
                                dataList.add(new CurveHistoryDataBackBean(datum.getDifName(), stringList));
                            }
                        }
                        getView().queryHistoryCurveDataSuccess(dataList);

                    }
                });
    }


    /**
     * 接口请求
     *
     * @param eSource   2 典型曲线  3自定义曲线  4 偏差曲线
     * @param isLoading
     */
    public void getData(int eSource, boolean isLoading) {

        Map<String, Object> params = new HashMap<>();
        params.put("eSource", eSource);
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_CURVE_INFO,
                new MvpNetCallback<CurveDataBean>(getView(), isLoading) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(CurveDataBean response, int id) {
                        super.onResponse(response, id);
                        Map<String, List<String>> maps = new HashMap<>();
                        if (null != response && null != response.getData() && response.getData().size() > 0) {
                            List<CurveDataBean.DataDTO> data = response.getData();
                            for (CurveDataBean.DataDTO datum : data) {
                                List<String> stringList = new ArrayList<>();
                                stringList.add(datum.getRatio1());
                                stringList.add(datum.getRatio2());
                                stringList.add(datum.getRatio3());
                                stringList.add(datum.getRatio4());
                                stringList.add(datum.getRatio5());
                                stringList.add(datum.getRatio6());
                                stringList.add(datum.getRatio7());
                                stringList.add(datum.getRatio8());
                                stringList.add(datum.getRatio9());
                                stringList.add(datum.getRatio10());
                                stringList.add(datum.getRatio11());
                                stringList.add(datum.getRatio12());
                                stringList.add(datum.getRatio13());
                                stringList.add(datum.getRatio14());
                                stringList.add(datum.getRatio15());
                                stringList.add(datum.getRatio16());
                                stringList.add(datum.getRatio17());
                                stringList.add(datum.getRatio18());
                                stringList.add(datum.getRatio19());
                                stringList.add(datum.getRatio20());
                                stringList.add(datum.getRatio21());
                                stringList.add(datum.getRatio22());
                                stringList.add(datum.getRatio23());
                                stringList.add(datum.getRatio24());
                                maps.put(datum.getRatioName(), stringList);
                            }
                        }

                        if (eSource == 4)
                            getView().queryDeviationCurveDataSuccess(maps);
                        else
                            getView().queryCurveDataSuccess(maps);


                    }
                });


    }

}
